"""
@Author  : 康帅
@Time    : 2021/5/15 9:36
@Function: 邮件发送装饰器
"""


import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '11111@qq.com'
receivers = ['11111@qq.com', '456789@163.com']
mail_host = 'smtp.qq.com'
mail_user = '11111@qq.com'
mail_pass = 'abc...'
message = MIMEText('邮件发送成功', 'plain', 'utf-8')
message['From'] = Header('小海', 'utf-8')
message['To'] = Header('小王', 'utf-8')
subject = '邮件发送成功'
message['Subject'] = Header(subject, 'utf-8')


def send_email(func):
    """ 发送邮件装饰器 """
    def wrapper(*args, **kwargs):
        try:
            obj = smtplib.SMTP_SSL(mail_host, 465)
            obj.login(mail_user, mail_pass)
            obj.sendmail(sender, receivers, message.as_string())
            print('邮件发送成功')
        except smtplib.SMTPException:
            print('邮件发送失败')
        func(*args, **kwargs)
    return wrapper


def send_email_data():
    """
    sender: 发送邮件的人
    receivers: 接收邮件人
    mail_host: 设置发送服务器
    mail_user: 登录邮箱名
    mail_pass: 口令（授权码）
    message: 发送邮件正文
    message['From']: 发送者
    message['To']: 接收者
    message['Subject']: 发送邮件标题
    """
    try:
        obj = smtplib.SMTP_SSL(mail_host, 465)  # 发送服务器的端口号,邮件默认是465
        obj.login(mail_user, mail_pass)
        obj.sendmail(sender, receivers, message.as_string())
        print('邮件发送成功')
    except smtplib.SMTPException:
        print('邮件发送失败')
